<template>
  <div class="product">
    <div
      class="product__item"
      v-for="item in productList"
      :key="item._id"
    >
      <img class="product__item__img" :src="item.imgUrl" />
      <div class="product__item__detail">
        <h4 class="product__item__title">{{item.name}}</h4>
        <p class="product__item__sales">月售 {{item.sales}} 件</p>
        <p class="product__item__price">
          <span class="product__item__yen">&yen;</span>{{item.price}}
          <span class="product__item__origin">&yen;{{item.oldPrice}}</span>
        </p>
      </div>
      <div class="product__number">
        <span
          v-show="getProductCartCount(shopId, item._id) > 0"
          class="product__number__minus"
          @click="() => { changeCartItem(shopId, item._id, item, -1) }"
        >-</span>
        <span v-show="getProductCartCount(shopId, item._id) > 0">
          {{getProductCartCount(shopId, item._id)}}
        </span>
        <span
          class="product__number__plus"
          @click="() => { changeCartItem(shopId, item._id, item, 1) }"
        >+</span>
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import { get } from '../../utils/request'

// 获取商品列表相关逻辑
const useProductListEffect = (shopId) => {
  const productList = ref([])
  const getProductList = async () => {
    const result = await get(`/api/shop/${shopId}/products`)
    if(result?.errno === 0 && result?.data?.length) {
      // 如果API调用失败，使用模拟数据
      productList.value = result.data
    } else {
      productList.value = [
        {
          _id: '1',
          name: '番茄 250g/份',
          imgUrl: '/imgs/products/tomato.png',
          sales: 10,
          price: 33.6,
          oldPrice: 39.6
        },
        {
          _id: '2',
          name: '提子 250g/份',
          imgUrl: '/imgs/products/grape.png',
          sales: 10,
          price: 33.6,
          oldPrice: 39.6
        }
      ]
    }
  }
  return { productList, getProductList }
}

// 购物车相关逻辑
const useCartEffect = () => {
  const store = useStore()
  const route = useRoute()
  const shopId = route.params.id
  
  const changeCartItem = (shopId, productId, item, num) => {
    store.commit('changeCartItemInfo', {
      shopId, productId, productInfo: item, num
    })
  }
  
  const getProductCartCount = (shopId, productId) => {
    return store.state.cartList?.[shopId]?.productList?.[productId]?.count || 0
  }
  
  return { shopId, changeCartItem, getProductCartCount }
}

export default {
  name: 'ProductList',
  setup() {
    const route = useRoute()
    const shopId = route.params.id
    const { productList, getProductList } = useProductListEffect(shopId)
    const { changeCartItem, getProductCartCount } = useCartEffect()
    
    getProductList()
    
    return {
      productList,
      shopId,
      changeCartItem,
      getProductCartCount
    }
  }
}
</script>

<style lang="scss" scoped>
@import '../../style/variables.scss';
.product {
  &__item {
    position: relative;
    display: flex;
    padding: .12rem 0;
    margin: 0 .16rem;
    border-bottom: .01rem solid $content-bgColor;
    &__detail {
      overflow: hidden;
    }
    &__img {
      width: .68rem;
      height: .68rem;
      margin-right: .16rem;
    }
    &__title {
      margin: 0;
      line-height: .2rem;
      font-size: .14rem;
      color: $content-fontcolor;
    }
    &__sales {
      margin: .06rem 0;
      font-size: .12rem;
      color: $content-fontcolor;
    }
    &__price {
      margin: 0;
      line-height: .2rem;
      font-size: .14rem;
      color: $hightlight-fontColor;
    }
    &__yen {
      font-size: .12rem;
    }
    &__origin {
      margin-left: .06rem;
      line-height: .2rem;
      font-size: .12rem;
      color: $light-fontColor;
      text-decoration: line-through;
    }
  }
  &__number {
    position: absolute;
    right: .16rem;
    bottom: .12rem;
    &__minus, &__plus {
      display: inline-block;
      width: .2rem;
      height: .2rem;
      line-height: .16rem;
      border-radius: 50%;
      font-size: .2rem;
      text-align: center;
    }
    &__minus {
      border: .01rem solid $medium-fontColor;
      color: $medium-fontColor;
      margin-right: .05rem;
    }
    &__plus {
      background: $btn-bgColor;
      color: $bgColor;
      margin-left: .05rem;
    }
  }
}
</style>
